<?php
/**
 * Created by PhpStorm.
 * User: teddy
 * Date: 5/4/16
 * Time: 4:54 PM
 */


// 设置
ini_set("display_errors", "On");
error_reporting(E_ALL | E_STRICT);

// IMPORT
require_once("JPush/JPush.php");

// 请求方法
$request_method = strtolower($_SERVER['REQUEST_METHOD']);
// 地理位置信息
$request_location = $_GET['location'];
$request_regID = $_GET['regID'];

print $request_location;



$br = '<br/>';
$app_key = 'c47bb9f1ef9c74ff8235a5a8';
$master_secret = '60da89d9d97bd260aea7028b';
$weather_api = 'https://api.thinkpage.cn/v3/weather/daily.json?key=sqcsdvosyk82l5sw&language=zh-Hans&unit=c&start=-1&days=5&location=';

$weather_get = $weather_api . $request_location;
$weather_get = iconv('gbk','UTF-8',$weather_get);

// print $weather_get;

// $weather_json = file_get_contents($weather_get);

$ch = curl_init();

// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL, $weather_get);
// Execute
$json=curl_exec($ch);
// Closing
curl_close($ch);

print ($json);

$jsonDecode = json_decode($json);


// 明天降雨概率
$precip1 = ($jsonDecode->results[0]->daily[1]->precip);

// 后天降雨概率
$precip2 = ($jsonDecode->results[0]->daily[2]->precip);


// 初始化
$client = new JPush($app_key, $master_secret);
// 简单推送示例
$result = $client->push()
    ->setPlatform('ios')
    ->addRegistrationId($request_regID)
    ->setNotificationAlert("明天降雨概率为" . $precip1 . ", 后天降雨概率为" . $precip2)
    ->send();
echo 'Result=' . json_encode($result) . $br;
// 完整的推送示例,包含指定Platform,指定Alias,Tag,指定iOS,Android notification,指定Message等
//$result = $client->push()
//    ->setPlatform(array('ios', 'android'))
//    ->addAlias('alias1')
//    ->addTag(array('tag1', 'tag2'))
//    ->setNotificationAlert('Hi, JPush')
//    ->addAndroidNotification('Hi, android notification', 'notification title', 1, array("key1"=>"value1", "key2"=>"value2"))
//    ->addIosNotification("Hi, iOS notification", 'iOS sound', JPush::DISABLE_BADGE, true, 'iOS category', array("key1"=>"value1", "key2"=>"value2"))
//    ->setMessage("msg content", 'msg title', 'type', array("key1"=>"value1", "key2"=>"value2"))
//    ->setOptions(100000, 3600, null, false)
//    ->send();
//echo 'Result=' . json_encode($result) . $br;
